import Ember from 'ember';
import BaseController from './base-controller';
import Changeset from 'ember-changeset';
import AssessmentValidations from '../../../validations/employeeachievementstype';
import AssessmentItemValidations from '../../../validations/employeeachievementstypeitem';
import lookupValidator from 'ember-changeset-validations';

export default BaseController.extend(AssessmentValidations, AssessmentItemValidations, {
  detailFlag: false,
  indicatorModel: null,
  inTabFlag:false,
  dataloader: Ember.inject.service('data-loader'),
  mainController: Ember.inject.controller('business.mainpage'),
  global_curStatus: Ember.inject.service("current-status"),
  childrenList: [],
  leftLoading: true,
  rightLoading: false,
  // childrenList:Ember.computed('curParent',function(){
  //   let allAssessmentList = this.get('allAssessmentList');
  //   if(!allAssessmentList){
  //     return ;
  //   }
  //   let list = new Ember.A();
  //   allAssessmentList.forEach(function(item){
  //       if(item.get('parent.id')==curParent.get('id')){
  //           list.pushObject(item);
  //         }
  //       });
  //   let curParent = this.get('curParent');
  //   return list;
  // }),
  assessmentModel:Ember.computed("newAssessment",function(){
    var model = this.get("newAssessment");
    if (!model) {
      return null;
    }
    return new Changeset(model, lookupValidator(AssessmentValidations), AssessmentValidations);
  }),
  assessmentItemModel:Ember.computed("newAssessmentItem",function(){
    var model = this.get("newAssessmentItem");
    if (!model) {
      return null;
    }
    return new Changeset(model, lookupValidator(AssessmentItemValidations), AssessmentItemValidations);
  }),
  actions: {
    //新增考核
    toAddParent(str){
      if(str=='child'){
        this.set('itemModelName','新增');
        this.set('childModel',true);
        this.set('newAssessmentItem',this.store.createRecord('employeeachievementstypeitem',{}));
      }else{
        this.set('modelName','新增');
        this.set('parentModel',true);
        this.set('newAssessment',this.store.createRecord('employeeachievementstype',{}));
      }
    },
    invitation(){
      this.set('parentModel',false);
      this.set('childModel',false);
      this.set('newAssessment',null);
    },
    //保存考核指标
    saveNewAssessment(str){
      let _self = this;
      let assessmentModel;
      if(str=="child"){
        assessmentModel = this.get('assessmentItemModel');
        assessmentModel.set('achievementsType', this.get('curParent'));
      }else{
        assessmentModel = this.get('assessmentModel');
        let employeeProformanceType;
        if (this.get("inTabFlag")) {
          employeeProformanceType = 'employeeProformanceType2';
        } else {
          employeeProformanceType = 'employeeProformanceType1';
        }
        let dictItem = this.get('dataloader').findDict(employeeProformanceType);
        console.log('dictItem::', employeeProformanceType, dictItem);
        assessmentModel.set('type', dictItem);
      }
      assessmentModel.set('delStatus', 0);
      // assessmentModel.set('maxScore',assessment.get('maxScore'));
      // assessmentModel.set('remark',assessment.get('remark'));
      assessmentModel.validate().then(function(){
        if(assessmentModel.get('errors.length')==0){
          App.lookup('controller:business.mainpage').openPopTip('正在保存');
          // let user = this.get("global_curStatus").getUser();
          assessmentModel.save().then(function(){
            _self.set('parentModel',false);
            _self.set('childModel',false);
            App.lookup('controller:business.mainpage').showPopTip('保存成功');
            // var route = App.lookup('route:business.mainpage.performance-management');
            // App.lookup('controller:business.mainpage').refreshPage(route);
            if(str=="child"){
              _self.send('getChildList');
            }else{
              _self.set("rightLoading", false);
              _self.set("childrenList", []);
              App.lookup("route:business.mainpage.performance-management").doQuery();
            }
          },function(err){
            App.lookup('controller:business.mainpage').showPopTip('保存失败',false);
          });
        }else{
          assessmentModel.set("validFlag",Math.random());
        }
      });
    },
    //选择考核指标
    chooseParent(assessment){
      const _self = this;
      this.get('assessmentList').forEach(function(item){
        item.set('hasChoosed',false);
      });
      assessment.set('hasChoosed',true);
      this.set('curParent',assessment);
      this.send('getChildList');
    },
    getChildList() {
      const _self = this;
      this.set('childrenList', []);
      this.set('rightLoading', true);
      let assessmentId = this.get('curParent.id');
      this.store.query('employeeachievementstypeitem',{
        filter: {
          achievementsType: {
            id: assessmentId,
          }
        }
      }).then(function(indicatorItemList){
        console.log('indicatorItemList:', indicatorItemList);
        _self.set('childrenList',indicatorItemList);
        _self.set('rightLoading',false);
      });     
    },
    //编辑指标名称
    detailEditClick(item){
      this.set('modelName','编辑');
      this.set('parentModel',true);
      this.set('newAssessment',item);
    },
    //编辑考核细节
    editChild(child){
      this.set('modelName','编辑');
      this.set('childModel',true);
      this.set('newAssessmentItem',child);
    },
    //删除指标
    delById(assessment, str){
      let _self = this;
      let msg = '';
      if(str=="child"){
        msg = '是否删除该规定细则';
      }else{
        msg = '是否删除该规定项目';
      }
    App.lookup('controller:business.mainpage').showConfirm(msg,function(){
        _self.send('cancelPassSubmit',assessment,str);
      });
    },
    cancelPassSubmit(item,str){
      let _self = this;
      item.set('delStatus',1);
      App.lookup('controller:business.mainpage').openPopTip('正在删除');
      item.save().then(function(){
        App.lookup('controller:business.mainpage').showPopTip("删除成功");
        if(str=="child"){
          _self.send('getChildList');
        }else{
          _self.set("rightLoading", false);
          _self.set("childrenList", []);
          App.lookup("route:business.mainpage.performance-management").doQuery();
        }
    // var route = App.lookup('route:business.mainpage.performance-management');
        // App.lookup('controller:business.mainpage').refreshPage(route);
      },function(err){
        let error = err.errors[0];
        if(error.code==8){
          App.lookup('controller:business.mainpage').showPopTip("不能删除有项目细则的规定项目",false);
        }
      });
    },
    //保存指标名称
    saveAssessment(assessment){
      assessment.save().then(function(){
        console.log('保存成功');
        assessment.set('edit',false);
      });
    },
    changeTab:function(inTabFlag){
      this.set("inTabFlag",inTabFlag);
      this.set("rightLoading", false);
      this.set("childrenList", []);
      App.lookup("route:business.mainpage.performance-management").doQuery();
    },
    //悬浮
    hoverAssessment(assessment){
      assessment.set('hover',true);
    },
    leaveAssessment(assessment){
      assessment.set('hover',false);
    },

  }
});
